<?php
/**
 * Laconica, the distributed open-source microblogging tool
 *
 * widget for displaying a list of notices
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  UI
 * @package   Laconica
 * @author    Evan Prodromou <evan@controlyourself.ca>
 * @author    Sarven Capadisli <csarven@controlyourself.ca>
 * @copyright 2008 Control Yourself, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://laconi.ca/
 */

if (!defined('LACONICA')) {
    exit(1);
}



class MessageList extends Widget
{

    var $message = null;
    var $messagelistsinbox = 1;
       


    function __construct($message, $out=null, $inbox=1)
    {
        parent::__construct($out);
        $this->message = $message;
        $this->messagelistsinbox =$inbox;
    }

    function show()
    {

        $this->out->elementStart('div', array('id' =>'notices_primary'));
        $this->out->element('h2', null, _('Notices'));
        $this->out->elementStart('ul', array('class' => 'notices'));

        $cnt = 0;

            while ($this->message->fetch() && $cnt <= MESSAGES_PER_PAGE) {
                $cnt++;

                if ($cnt > MESSAGES_PER_PAGE) {
                    break;
                }


            //Can xem lai cho dat thong so inbox, outbox o day
            $item = $this->newListItem($this->message,$this->messagelistsinbox);
            $item->show();
        }

        $this->out->elementEnd('ul');
        $this->out->elementEnd('div');

        return $cnt;
    }


    function newListItem($message, $inbox)
    {
        return new MessageListItem($message, $this->out, $inbox);
    }
}


class MessageListItem extends Widget
{
    var $message = null;
    var $isinbox = 1;
    var $profile = null;


    function __construct($message, $out=null, $inbox=1)
    {
        parent::__construct($out);
        $this->message  = $message;
        $this->isinbox = $inbox;
        if($inbox==1)
        {
           $this->profile = $this->message->getFrom();
        }
        else
        {
            $this->profile = $this->message->getTo();
        }

    }
    
    function show()
    {
        $this->out->elementStart('li', array('class' => 'hentry notice',
                                         'id' => 'message-' . $this->message->id));
        $this->out->elementStart('div', 'entry-title');
        $this->out->elementStart('span', 'vcard author');
        $this->out->elementStart('a', array('href' => $this->profile->profileurl,
                                       'class' => 'url'));
        $avatar = $this->profile->getAvatar(AVATAR_STREAM_SIZE);
        $this->out->element('img', array('src' => ($avatar) ?
                                    $avatar->displayUrl() :
                                    Avatar::defaultImage(AVATAR_STREAM_SIZE),
                                    'class' => 'photo avatar',
                                    'width' => AVATAR_STREAM_SIZE,
                                    'height' => AVATAR_STREAM_SIZE,
                                    'alt' =>
                                    ($this->profile->fullname) ? $this->profile->fullname :
                                    $this->profile->nickname));
        $this->out->element('span', array('class' => 'nickname fn'),
                            $this->profile->nickname);
        $this->out->elementEnd('a');
        $this->out->elementEnd('span');

        // FIXME: URL, image, video, audio
        $this->out->elementStart('p', array('class' => 'entry-content'));
        $this->out->raw($this->message->rendered);
        $this->out->elementEnd('p');
        $this->out->elementEnd('div');
        $this->showMessageOptions();
        $messageurl = common_local_url('showmessage',
                                       array('message' => $this->message->id));

        // XXX: we need to figure this out better. Is this right?
        if (strcmp($this->message->uri, $messageurl) != 0 &&
            preg_match('/^http/', $this->message->uri)) {
            $messageurl = $this->message->uri;
        }

        $this->out->elementStart('div', 'entry-content');
        $this->out->elementStart('dl', 'timestamp');
        $this->out->element('dt', null, _('Published'));
        $this->out->elementStart('dd', null);
        $dt = common_date_iso8601($this->message->created);
        $this->out->elementStart('a', array('rel' => 'bookmark',
                                       'href' => $messageurl));
        $this->out->element('abbr', array('class' => 'published',
                                     'title' => $dt),
                               common_date_string($this->message->created));
        $this->out->elementEnd('a');
        $this->out->elementEnd('dd');
        $this->out->elementEnd('dl');
        
        //$this->out->raw($this->message->from_profile." Va nguon nhan ".$this->message->to_profile);

        if ($this->message->source) {
            $this->out->elementStart('dl', 'device');
            $this->out->elementStart('dt');
            $this->out->text(_('From'));
            $this->out->elementEnd('dt');
            $this->showSource($this->message->source);
            $this->out->elementEnd('dl');
        }
        $this->out->elementEnd('div');

        $this->out->elementEnd('li');
    }
    
     function showSource($source) 
    {
        $source_name = _($source);
        switch ($source) {
        case 'web':
        case 'xmpp':
        case 'mail':
        case 'omb':
        case 'api':
            $this->out->element('dd', null, $source_name);
            break;
        default:
            $ns = Notice_source::staticGet($source);
            if ($ns) {
                $this->out->elementStart('dd', null);
                $this->out->element('a', array('href' => $ns->url,
                                          'rel' => 'external'),
                               $ns->name);
                $this->out->elementEnd('dd');
            } else {
                $this->out->element('dd', null, $source_name);
            }
            break;
        }
        return;
    }
    
    function showMessageOptions()
    {
        $user = common_current_user();
        if ($user) {
            $this->out->elementStart('div', 'notice-options');
            $this->showDeleteLink();
            $this->out->elementEnd('div');
        }
    }
    
    function showDeleteLink()
    {
        $user = common_current_user();
        if (($user && (($this->message->from_profile == $user->id)||($this->message->to_profile == $user->id)))) {
            $deleteurl = common_local_url('deletemessage',
                                          array('message' => $this->message->id));
            $this->out->elementStart('dl', 'notice_delete');
            $this->out->element('dt', null, _('Xoá tin nhắn cá nhân này'));
            $this->out->elementStart('dd');
            $this->out->element('a', array('href' => $deleteurl,
                                           'title' => _('Xoá tin nhắn cá nhân này')), _('Delete'));
            $this->out->elementEnd('dd');
            $this->out->elementEnd('dl');
        }
    }

    

}
